
#include	"SysConfig.h"
#include	"Define.h"
#include	"Port.h"
#include	"Model_Option.h"
#include	"Function.h"
#include	"Audio.h"
#include	"Display.h"
#ifdef  HERO
#include	"Hero_Drv.h"
#endif
#ifdef  ST7565P
#include	"ST7565P.h"
#endif
#ifdef  MIC
#include	"MIC.h"
#endif
#ifdef  AV_CONTROLLER
#include	"AVController.h"
#endif
#ifdef  HEF4052
#include	"HEF_4052.h"
#endif

#ifdef  AUTO_GUIDE
#include	"AutoGuide.h"

unsigned char ucAG_Process;
#define	AG_PRS_IDLE						0x00
#define	AG_PRS_RESET					0x01
#define	AG_PRS_NORMAL					0x02
#define	AG_PRS_CHANGE_ANNOUNCE			0x03
#define	AG_PRS_CHANGE_NORMAL			0x04
#define	AG_PRS_CHANGE_ANNOUNCE_REL_MUTE	0x05
unsigned short usAG_Process_Timer10msec;

unsigned char ucAG_AnnounceCheckCnt;
AG_ANNOUNCE_STATUS ucAG_AnnounceStatus;
MIC_STATUS ucAG_MicMode;

unsigned char ucAG_MicCheckCnt;
unsigned char ucAG_Volume;

extern unsigned char ucAUDIO_Volume;
extern void AUDIO_SetMute(unsigned char ucDes);
extern void FUNC_AudioVolume(UI_FUNCTION ucFunc);

AG_ANNOUNCE_STATUS AG_GetAnnounceStatus(void)
{
	return(ucAG_AnnounceStatus);
}

MIC_STATUS AG_GetMicStatus(void)
{
	return(ucAG_MicMode);
}

void AG_SetVolume(void)
{
	#ifdef  HERO
	if  (ucAG_Volume != 0)
	{
		LOG_SYS("AUDIO_VolumeChange:%d",ucAG_Volume);
		HERO_SetVolume(ucAG_Volume);
	}
	#endif
	#ifdef  LC786XXX_AUDIO
//	DRV_LC786XXX_VOL_ControlCommand(ucAUDIO_Volume);
	DRV_LC786XXX_WriteEVRGain(ucAG_Volume, ucAUDIO_Fader, ucAUDIO_Balance);
	#endif
}

void AG_VolumeChange(unsigned char ucDes)
{
	unsigned char ucVol;
	unsigned char ucLoudChange = 0;
	
	if  (ucDes == 1)
	{
		if  (ucAG_Volume == 0)
		{
			AUDIO_MuteOff();// Mute Off
		}
		if  (++ucAG_Volume > VOLUME_MAX)
		{
			ucAG_Volume = VOLUME_MAX;
		}
	}
	else
	{
		if  (ucAG_Volume != 0)
		{
			--ucAG_Volume;
		}
		if  (ucAG_Volume == 0)
		{
			AUDIO_MuteOn();// Mute On
		}
	}

	DISP_SetTempDisplayMode(DISP_AG_VOLUME);
	
	#ifdef  HERO
	if  (ucAG_Volume != 0)
	{
		LOG_SYS("AUDIO_VolumeChange:%d",ucAG_Volume);
		HERO_SetVolume(ucAG_Volume);
	}
	#endif
}

void AG_SetMicStatus(MIC_STATUS ucMode)
{
	ucAG_MicMode = ucMode;
}

void AG_ProcessStart(void)
{
	usAG_Process_Timer10msec = 0;//1000/10;
	PO_AG_RST = 0;
	ucAG_Process = AG_PRS_RESET;
}

void AG_ProcessStop(void)
{
	ucAG_Process = AG_PRS_IDLE;
	usAG_Process_Timer10msec = 0;
	PO_AG_RST = 0;
	
	ucAG_AnnounceStatus = AG_ANNOUNCE_OFF;
//	AG_SetMicStatus(MIC_OFF);
}

void AG_SetAnnounce(void)
{
	ucAG_Volume = 12;
	ucAG_Process = AG_PRS_CHANGE_ANNOUNCE;
	usAG_Process_Timer10msec = 50/10;
	AUDIO_SetMute(6);
	DISP_ClearTempDisplayMode();
	MENU_Clear();
	ucAG_AnnounceStatus = AG_ANNOUNCE_ON;
}

void AG_AnnounceCheck(void)
{
	if (PI_AG_ANC == AG_ANC_PORT_ON)
	{
		if  (AG_GetAnnounceStatus() == AG_ANNOUNCE_OFF)
		{
			if  (++ucAG_AnnounceCheckCnt > 3)
			{
				ucAG_AnnounceCheckCnt = 0;

				if  (AG_GetMicStatus() == MIC_OFF)
				{
					AG_SetAnnounce();
				}
			}
		}
	}
	else
	{
		if  (AG_GetAnnounceStatus() == AG_ANNOUNCE_ON)
		{
			ucAG_AnnounceStatus = AG_ANNOUNCE_OFF;
			ucAG_AnnounceCheckCnt = 0;
			DISP_ClearTempDisplayMode();
			MENU_Clear();
			ucAG_Process = AG_PRS_CHANGE_NORMAL;
			usAG_Process_Timer10msec = 50/10;
			AUDIO_SetMute(6);
		}
	}
}

void AG_MicCheck(void)
{
	if (PI_AG_MIC == AG_MIC_PORT_ON)
	{
		if  (AG_GetMicStatus()== MIC_OFF)
		{
		//	if  (++ucAG_MicCheckCnt > 3)
			{
				if  (FUNC_PowerStatus())
				{
					AG_SetMicStatus(MIC_ON);
					if  (MIC_GetMicStatus() == MIC_OFF)
					{
				//		HERO_PhoneOnOff(1);
						HERO_PhoneMix(0);
						MIC_SetMicStatus(MIC_ON);
					}
				}
				ucAG_MicCheckCnt = 0;
			}
		}
	}
	else
	{
		if  (AG_GetMicStatus()== MIC_ON)
		{
			AG_SetMicStatus(MIC_OFF);
			ucAG_MicCheckCnt = 0;
		}
	}
}

void AG_AutoGuideMain(void)
{
	if  (OPT_GetOption(OPT_MASK_AG) == 0)
	{
		return;
	}
	
	if  (usAG_Process_Timer10msec != 0)
	{
		return;
	}	

	switch  (ucAG_Process)
	{
		case AG_PRS_IDLE:
			break;
		case AG_PRS_RESET:
			ucAG_Process = AG_PRS_NORMAL;
			usAG_Process_Timer10msec = 100/10;
			PO_AG_RST = 1;
			break;
		case AG_PRS_NORMAL:		// Port check
			AG_MicCheck();
			AG_AnnounceCheck();
			break;
		case AG_PRS_CHANGE_ANNOUNCE:
			ucAG_Process = AG_PRS_CHANGE_ANNOUNCE_REL_MUTE;
			usAG_Process_Timer10msec = 50/10;

			if  ((MIC_GetMicStatus() == MIC_ON) && (MIC_GetMixMode() == MIX_OFF))
			{
				HERO_PhoneOnOff(0);
			}
			AUDIO_InputSelect(FUNC_GetCurrentFunction());
			AG_SetVolume();
			
			AVC_SetKeyCommand(AVC_KEY_ROUTE_GUIDE_ON);

		#if  0
			if  (FUNC_GetCurrentFunction() == FUNC_USB)
			{
				USB_PauseOff(0);
				#ifdef  LC786XXX
				if  (LC786XXX_GetMediaStatus(FUNC_USB) == LC786X_MS_PLAYBACK)
				{
					DRV_LC786XXX_Playback(LC786X_USB, 0x02);
				}
				#endif
			}
			if  (FUNC_GetCurrentFunction() == FUNC_CD)
			{
				CD_PauseOff(0);
			#ifdef  LC78696X
				if  (LC786XXX_GetMediaStatus(FUNC_CD) == LC786X_MS_PLAYBACK)
				{
					if  (ucCD_CurrentPlayDiscType == CD_DISC_CDROM)
					{
						DRV_LC786XXX_Playback(LC786X_CDROM, 0x02);	// Pause
					}
					else
					{
						DRV_LC786XXX_Playback(LC786X_CDDA, 0x02);	// Pause
					}
				}
			#endif
			}
		#endif
			break;
		case AG_PRS_CHANGE_NORMAL:
			ucAG_Process = AG_PRS_CHANGE_ANNOUNCE_REL_MUTE;
			usAG_Process_Timer10msec = 50/10;

			if  (MIC_GetMicStatus() == MIC_ON)
			{
				HERO_PhoneOnOff(1);
				if  (MIC_GetMixMode()== MIX_ON)
				{
					MIC_SetMixMode(MIX_ON);
				}
			}
			else
			{
				AUDIO_InputSelect(FUNC_GetCurrentFunction());
			}
			FUNC_AudioVolume(FUNC_GetCurrentFunction());
			AVC_SetKeyCommand(AVC_KEY_ROUTE_GUIDE_OFF);
		#if  0
			if  (FUNC_GetCurrentFunction() == FUNC_USB)
			{
				#ifdef  LC786XXX
				if  (LC786XXX_GetMediaStatus(FUNC_USB) == LC786X_MS_PLAYBACK)
				{
					DRV_LC786XXX_Playback(LC786X_USB, 0x01);
				}
				#endif
			}
			if  (FUNC_GetCurrentFunction() == FUNC_CD)
			{
			#ifdef  LC78696X
				if  (LC786XXX_GetMediaStatus(FUNC_CD) == LC786X_MS_PLAYBACK)
				{
					if  (ucCD_CurrentPlayDiscType == CD_DISC_CDROM)
					{
						DRV_LC786XXX_Playback(LC786X_CDROM, 0x01);
					}
					else
					{
						DRV_LC786XXX_Playback(LC786X_CDDA, 0x01);
					}
				}
			#endif
			}
		#endif
			break;
		case AG_PRS_CHANGE_ANNOUNCE_REL_MUTE:
			ucAG_Process = AG_PRS_NORMAL;
			AUDIO_SetMute(5);
			break;
	}
}

void AG_DISP_MainText(void)
{
	unsigned char ucBuf[20];

	memset(&ucBuf[0], 0x00, 20);

	memcpy(&ucBuf[0], "R/T GUIDE", 9);
#ifdef  ST7565P
	ST7565_UpdateDisplayString(8, &ucBuf[0], 0);
#endif
}

void AG_DISP_IconMain(void)
{
}

void AG_DISP_SubText(void)
{	
	unsigned char ucBuf[3];

	memset(&ucBuf[0], 0x00, 3);
	memcpy(&ucBuf[0], "RT", 2);
	
#ifdef  ST7565P
	ST7565_UpdateSubDisplayData(&ucBuf[0]); 
#endif
}

void AG_DISP_Volume(void)
{
	unsigned char ucBuf[15];

	memset(&ucBuf[0], ' ', 15);

#ifdef  ST7565P
	if	(AG_GetAnnounceStatus() == AG_ANNOUNCE_ON)
	{
		ucBuf[0] = 'R';
		ucBuf[1] = '/';
		ucBuf[2] = 'T';
		ucBuf[3] = ' ';
		ucBuf[4] = 'V';
		ucBuf[5] = 'O';
		ucBuf[6] = 'L';
		ucBuf[7] = 0x00;
		
		ucBuf[8] = (ucAG_Volume / 10) | '0';
		ucBuf[9] = (ucAG_Volume % 10) | '0';
		ucBuf[10] = 0x00;

		ST7565_UpdateDisplayString(8, &ucBuf[0], 0);	
		ST7565_UpdateDisplayString(70,&ucBuf[8], 0);	
		return;
	}
#endif
}

#endif
